/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VentanaEstadisticasTienda.java
 *
 * Created on 16/01/2009, 10:05:06 AM
 */

package com.wd.gui;

import com.wd.dominio.Inventario;
import com.wd.dominio.Producto;
import com.wd.dominio.Tienda;
import com.wd.gui.controlparticular.ControlGuiInventario;
import com.wd.gui.controlparticular.ControlGuiProducto;
import com.wd.gui.controlparticular.ControlGuiTienda;
import java.util.Collection;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author gerardo
 */
public class VentanaEstadisticasTienda extends javax.swing.JFrame {

    private Collection<Tienda> tiendasMayor;

    private Collection<Tienda> tiendasMenor;
    
    private Vector<Tienda> vecTiendasAll = new Vector();
    
    private Vector<Tienda> vecTiendas = new Vector();

    private Vector<Producto> vecProdsAll = new Vector();

    private Vector<Producto> vecProdsAllAux = new Vector();

    private Vector<Inventario> vecProdsInv = new Vector();

    private ControlGuiProducto controlProducto = new ControlGuiProducto();

    private ControlGuiInventario controlInventario = new ControlGuiInventario();

    private ControlGuiTienda controlTienda = new ControlGuiTienda();

    /** Creates new form VentanaEstadisticasTienda */
    public VentanaEstadisticasTienda() {
        initComponents();

        tiendasMayor = controlTienda.traerTiendasMayorTamano();

        tiendasMenor = controlTienda.traerTiendasMenorTamano();

         DefaultTableModel dm = new DefaultTableModel();
            dm.addColumn("Codigo");
            dm.addColumn("Nombre");
            dm.addColumn("Tamaño");    

      DefaultTableModel dm2 = new DefaultTableModel();
            dm2.addColumn("Codigo");
            dm2.addColumn("Nombre");
            dm2.addColumn("Tamaño");

       for1:
           for (Tienda tienda : tiendasMayor) {
           Vector info = new Vector();
                info.addElement(tienda.getCodigo());
                info.addElement(tienda.getNombre());
                info.addElement(tienda.getTamano());
                dm.addRow(info);
       }
           this.tablaMaMe.setModel(dm);

       for2:
           for (Tienda tienda : tiendasMenor) {
           Vector info = new Vector();
                info.addElement(tienda.getCodigo());
                info.addElement(tienda.getNombre());
                info.addElement(tienda.getTamano());
                dm2.addRow(info);
       }
           this.TablaMeMa.setModel(dm2);

       this.llenarTablaProdsMayorInvTiendas();


       this.llenarSurtidoAsc();
       this.llenarSurtidoDesc();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        TablaMeMa = new javax.swing.JTable();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tablaMaMe = new javax.swing.JTable();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        tablaMayorInventarioTiendas = new javax.swing.JTable();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        tablaMayorSurtido = new javax.swing.JTable();
        jPanel6 = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        tablaMenorSurtido = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();

        setTitle("Estadísticas Tienda");
        setAlwaysOnTop(true);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Estadísticas de Tiendas", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), null)); // NOI18N

        jTabbedPane1.setTabLayoutPolicy(javax.swing.JTabbedPane.SCROLL_TAB_LAYOUT);

        TablaMeMa.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(TablaMeMa);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 548, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 404, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(45, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("por Tamaño Menor a Mayor", jPanel3);

        tablaMaMe.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane2.setViewportView(tablaMaMe);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 548, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 404, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(45, Short.MAX_VALUE))
        );

        jTabbedPane1.addTab("por Tamaño Mayor a Menor", jPanel2);

        tablaMayorInventarioTiendas.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jScrollPane3.setViewportView(tablaMayorInventarioTiendas);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 568, Short.MAX_VALUE)
            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel4Layout.createSequentialGroup()
                    .addGap(27, 27, 27)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 531, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 460, Short.MAX_VALUE)
            .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel4Layout.createSequentialGroup()
                    .addGap(10, 10, 10)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(48, Short.MAX_VALUE)))
        );

        jTabbedPane1.addTab("Productos con Mayor Inventario", jPanel4);

        jScrollPane4.setViewportView(tablaMayorSurtido);

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 548, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 438, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("por Mayor Surtido", jPanel5);

        jScrollPane5.setViewportView(tablaMenorSurtido);

        javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
        jPanel6.setLayout(jPanel6Layout);
        jPanel6Layout.setHorizontalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 568, Short.MAX_VALUE)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 548, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel6Layout.setVerticalGroup(
            jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 460, Short.MAX_VALUE)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 438, Short.MAX_VALUE)
                .addContainerGap())
        );

        jTabbedPane1.addTab("por Menor Surtido", jPanel6);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 573, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 488, Short.MAX_VALUE)
                .addContainerGap())
        );

        jButton1.setText("Cerrar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(288, 288, 288)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        this.setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VentanaEstadisticasTienda().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTable TablaMeMa;
    private javax.swing.JButton jButton1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTable tablaMaMe;
    private javax.swing.JTable tablaMayorInventarioTiendas;
    private javax.swing.JTable tablaMayorSurtido;
    private javax.swing.JTable tablaMenorSurtido;
    // End of variables declaration//GEN-END:variables

    public void llenarVecs(){

        this.vecProdsAll = this.controlProducto.traerTodosLosProductos();
        this.vecProdsInv = this.controlInventario.traerProductosMayorInvTiendas();
        this.vecTiendasAll = controlTienda.consultarTiendas();

        for (Inventario inv : vecProdsInv) {
            for (Producto prod : vecProdsAll) {
                if (inv.getProductoId() == prod.getId()){
                    this.vecProdsAllAux.addElement(prod);
                }
            }
        }
        for (Inventario inv : vecProdsInv) {
            for (Tienda tiendita : vecTiendasAll) {
                if (inv.getTiendaCodigo() == tiendita.getCodigo()){
                    this.vecTiendas.addElement(tiendita);
                }
            }
        }
    }

    public void llenarTablaProdsMayorInvTiendas(){

        this.llenarVecs();

        DefaultTableModel dm = new DefaultTableModel();
        dm.addColumn("Código Tienda");
        dm.addColumn("NombreTienda ");
        dm.addColumn("Código Producto");
        dm.addColumn("Nombre Producto");
        dm.addColumn("Cantidad");

        Inventario inv = new Inventario();
        Producto prod = new Producto();
        Tienda store= new Tienda();
        for (int i = 0; i < vecProdsInv.size(); i++) {
            inv = vecProdsInv.elementAt(i);
            prod = vecProdsAllAux.elementAt(i);
            store = vecTiendas.elementAt(i);

            Vector info = new Vector();
            info.addElement(store.getCodigo());
            info.addElement(store.getNombre());
            info.addElement(prod.getId());
            info.addElement(prod.getNombre());
            info.addElement(inv.getCantidad());

            dm.addRow(info);
        }
        this.tablaMayorInventarioTiendas.setModel(dm);
    }

    private void llenarSurtidoAsc(){
        this.controlTienda = new ControlGuiTienda();
        DefaultTableModel dm = new DefaultTableModel();
        dm.addColumn("Tienda");
        dm.addColumn("Productos Disponibles");
        Vector<Tienda> tiendas = this.controlTienda.consultarTiendasVariedadAsc();
        for (Tienda tienda : tiendas) {
            Vector info = new Vector();
            info.addElement(tienda.getNombre());
            info.addElement(tienda.getVariedad());
            dm.addRow(info);
        }
        this.tablaMenorSurtido.setModel(dm);
    }

    private void llenarSurtidoDesc(){
        this.controlTienda = new ControlGuiTienda();
        DefaultTableModel dm = new DefaultTableModel();
        dm.addColumn("Tienda");
        dm.addColumn("Productos Disponibles");
        Vector<Tienda> tiendas = this.controlTienda.consultarTiendasVariedadDesc();
        for (Tienda tienda : tiendas) {
            Vector info = new Vector();
            info.addElement(tienda.getNombre());
            info.addElement(tienda.getVariedad());
            dm.addRow(info);
        }
        this.tablaMayorSurtido.setModel(dm);
    }


}
